﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.API.Applications.Command.AquacultureCommand;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.AquacultureCommandHandler
{
    public class FarmQueryCommandHandler : IRequestHandler<FarmQueryCommand, APIResult<Farm>>
    {
        private readonly IBaseRepository<Farm> farmRepository;

        public FarmQueryCommandHandler(IBaseRepository<Farm> farmRepository)
        {
            this.farmRepository = farmRepository;
        }

        public Task<APIResult<Farm>> Handle(FarmQueryCommand request, CancellationToken cancellationToken)
        {
           var res= farmRepository.GetAll().AsNoTracking().FirstOrDefault(s=>!s.IsDeltede&&s.Batch==request.Batch);

            APIResult<Farm> aPIResult = new APIResult<Farm>()
            {
                Code = EnumCode.Ok,
                Msg="显示成功",
                Data=res,
            };
            return Task.FromResult(aPIResult);
        }
    }
}
